package vg.services.user_interface_manager.realization;

import javax.swing.JComponent;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;

import vg.services.user_interface_manager.interfaces.IInstruments;

/**
 * This class implements IIntruments. 
 * @author tzolotuhin
 */
public class Instruments implements IInstruments {
	// Main components
	private final JToolBar view;
	// Mutex
	private final Object theMutexObject;
	/**
	 * Constructor.
	 */
	public Instruments() {
		// init mutex
		this.theMutexObject = new Object();
		// init components
		this.view = new JToolBar();
		this.view.setFloatable(false);
	}
	///////////////////////////////////////////////////////////////////////////
	// IMPLIMINTATION OF IINSTRUMENT
	///////////////////////////////////////////////////////////////////////////
	public JComponent getView() {
		return(this.view);
	}
	/**
	 * This method adds new instrument.
	 */
	public void addInstrument(final JComponent element) {
		if(element == null) return;
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				synchronized (Instruments.this.theMutexObject) {	
					Instruments.this.view.add(element);
					Instruments.this.view.addSeparator();
					Instruments.this.view.updateUI();
				}				
			}
		});
	}
	/**
	 * This method updates UI style of instruments.
	 */
	public void updateUIStyle() {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				synchronized (Instruments.this.theMutexObject) {
					SwingUtilities.updateComponentTreeUI(Instruments.this.view);
				}
			}
		});
	}
}
